package com.lsf.studentmanagerup.servlet.classbean;

import com.lsf.studentmanagerup.bean.ClassBean;
import com.lsf.studentmanagerup.service.ClassBeanService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李树飞
 */
@WebServlet(name = "GetClassBeanServlet", urlPatterns = "/getClassBean")
public class GetClassBeanServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //获取条件查询参数并以Map形式保存
        Map<String, String> condition = new HashMap<>();
        condition.put("id", request.getParameter("id"));
        condition.put("name", request.getParameter("name"));
        condition.put("grade", request.getParameter("grade"));
        condition.put("teacher", request.getParameter("teacher"));
        condition.put("slogen", request.getParameter("slogen"));
        condition.put("num", request.getParameter("num"));

        //调用班级服务层方法
        ClassBeanService classBeanService = new ClassBeanService();
        List<ClassBean> classBeanList = classBeanService.getClassBeanService(condition);

        //将查到的列表和查询条件存入到request
        request.setAttribute("classBeanList", classBeanList);
        request.setAttribute("condition", condition);

        request.getRequestDispatcher("class.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
